package com.bds.tjj.snatch;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.DaoException;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.NutDao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bds.tjj.http.HttpBase;
import com.bds.tjj.model.PropIndex;
import com.bds.tjj.model.QuarterAreaData;
import com.bds.tjj.model.QuarterData;
import com.bds.tjj.utils.SpringContextUtil;

/**
 * 按月抓取
 */

public class StatsQuarterThread extends Thread {

	private NutDao dao = SpringContextUtil.getBean("dao", NutDao.class);

	private static Logger logger = Logger.getLogger(StatsQuarterThread.class);

	private static String[] codes = { "A0501", "A0502", "A0104" };

	@Override
	public void run() {

		try {
			this.startCrawl();
		} catch (Exception e) {
			logger.error("", e);
		}
	}

	// 抓取
	// public static void main(String[] args) throws Exception {
	public void startCrawl() throws Exception {

		snatchQLast();
		
		snatchQE0102();

		snatchQA0104();

		

	}

	public void snatchQLast() throws Exception {
		List<String> big_list = new ArrayList<String>();
		List<Record> list = dao.query("T_PROP_INDEX",
				Cnd.where("chart_type", "=", "quarter").and("is_area", "=", "0").and("status", "=", 1));
		for (Record r : list) {
			if (!big_list.contains(r.getString("cn_name"))) {
				big_list.add(r.getString("cn_name"));
			}
		}
		for (String code : codes) {
			String url_no_area = "http://data.stats.gov.cn/easyquery.htm?m=QueryData&dbcode=hgjd&rowcode=zb&colcode=sj&wds=%5B%5D&dfwds=%5B%7B%22wdcode%22%3A%22zb%22%2C%22valuecode%22%3A%22"
					+ code + "%22%7D%5D&k1=" + new Date().getTime();
			String cookie_no_area = "_trs_uv=jd1hcdms_6_5v09; JSESSIONID=CFD49D78CBFAB32995A68A1DD331157F; u=6";
			snatchQ(big_list, url_no_area, "http://data.stats.gov.cn/easyquery.htm?cn=B01", cookie_no_area, null, null);

		}
	}

	public void snatchQA0104() throws Exception {
		List<String> big_list = new ArrayList<String>();
		big_list.add("国内生产总值环比增长速度");
		String url = "http://data.stats.gov.cn/easyquery.htm?m=QueryData&dbcode=hgjd&rowcode=zb&colcode=sj&wds=%5B%5D&dfwds=%5B%7B%22wdcode%22%3A%22sj%22%2C%22valuecode%22%3A%222011-%22%7D%5D&k1="
				+ new Date().getTime();
		String cookie = "_trs_uv=jd1hcdms_6_5v09; JSESSIONID=01F53C48D852470980940C8284115A9F; u=1";

		String ref = "ttp://data.stats.gov.cn/easyquery.htm?cn=B01";
		snatchQ(big_list, url, cookie, ref, null, null);
	}

	public void snatchQE0102() throws Exception {
		List<String> big_list = new ArrayList<String>();
		List<Record> list = dao.query("T_PROP_INDEX", Cnd.where("chart_type", "=", "quarter").and("is_area", "=", "1"));
		for (Record r : list) {
			if (!big_list.contains(r.getString("cn_name"))) {
				big_list.add(r.getString("cn_name"));
			}
		}
		Record area = dao.fetch("T_AREA", Cnd.where("id", "=", 1));
		String areas = area.getString("areas");

		JSONObject areaJson = JSON.parseObject(areas);
		JSONArray nodes = areaJson.getJSONArray("nodes");
		for (int i = 0; i < nodes.size(); i++) {
			JSONObject node = nodes.getJSONObject(i);
			String area_name = node.getString("cname");
			String area_code = node.getString("code");
			String ulr_area = "http://data.stats.gov.cn/easyquery.htm?m=QueryData&dbcode=fsjd&rowcode=zb&colcode=sj&wds=%5B%7B%22wdcode%22%3A%22reg%22%2C%22valuecode%22%3A%22"
					+ area_code
					+ "%22%7D%5D&dfwds=%5B%7B%22wdcode%22%3A%22sj%22%2C%22valuecode%22%3A%222005-%22%7D%5D&k1="
					+ new Date().getTime();

			String cookie_area = "_trs_uv=jd1hcdms_6_5v09; JSESSIONID=9C57CC33F0F0A47C83FC3F061C983F1A; u=1";
			snatchQ(big_list, ulr_area, cookie_area, "http://data.stats.gov.cn/easyquery.htm?cn=E0102", area_code,
					area_name);

		}

	}

	public void snatchQ(List<String> names, String url, String cookie, String referer, String area_code,
			String area_name) throws Exception {
		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Host", "data.stats.gov.cn");
		headers.put("Referer", referer);
		headers.put("User-Agent",
				"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");

		// 陶宝的老方法 一个月抓一次用
		headers.put("Cookie", cookie);
		String html3 = new HttpBase().get(url, "utf-8", headers);

		JSONObject d = JSON.parseObject(html3);
		// System.out.println(html3);
		JSONArray d10 = d.getJSONObject("returndata").getJSONArray("wdnodes").getJSONObject(0).getJSONArray("nodes");
		for (int p = 0; p < d10.size(); p++) {
			JSONObject e1010 = d10.getJSONObject(p);
			String code = e1010.getString("code");
			String sub = e1010.getString("name");
			// 筛选条件 (按名字)
			if (!names.contains(sub)) {
				continue;
			}
			JSONArray f10 = d.getJSONObject("returndata").getJSONArray("datanodes");
			for (int o = 0; o < f10.size(); o++) {
				JSONObject f1010 = f10.getJSONObject(o);
				String zbcode = null;
				String date = null;
				String regCode = null;
				JSONArray datas = f1010.getJSONArray("wds");
				for (int j = 0; j < datas.size(); j++) {
					JSONObject ds = datas.getJSONObject(j);
					String wdcode = ds.getString("wdcode");
					if (wdcode.equals("zb")) {
						zbcode = ds.getString("valuecode");
					} else if (wdcode.equals("reg")) {
						regCode = ds.getString("valuecode");
					} else if (wdcode.equals("sj")) {
						date = ds.getString("valuecode");
					}
				}
				if (zbcode.equals(code)) {

					if (StringUtils.isEmpty(area_code) || StringUtils.isEmpty(area_name)) {
						PropIndex prop = dao.fetch(PropIndex.class,
								Cnd.where("cn_name", "=", sub).and("is_area", "=", "0"));

						String strdata = f1010.getJSONObject("data").getString("strdata");

						String ndate = date.replace("A", "01");
						ndate = ndate.replace("B", "04");
						ndate = ndate.replace("C", "07");
						ndate = ndate.replace("D", "10");
						QuarterData tmd = new QuarterData();
						tmd.setCn_name(sub);
						tmd.setCreate_time(new Date());
						tmd.setData(strdata);
						tmd.setEn_name(prop.getEn_name());
						tmd.setYq(Integer.parseInt(ndate));
						tmd.setProp(prop.getProp());

						try {
							// dao.fastInsert(gi);
							dao.fastInsert(tmd);
							logger.info(sub + "," + strdata + "," + ndate);
						} catch (DaoException e) {
							// 报错就更新 (一般是重复异常)
							QuarterData mm = dao.fetch(QuarterData.class,
									Cnd.where("cn_name", "=", sub).and("yq", "=", ndate));
							if (mm == null) {
								logger.info("月度为空,:" + sub + "," + strdata);
							}
							if (StringUtils.isNotEmpty(strdata)) {
								if (mm.getData() != null) {
									if (!strdata.equals(mm.getData())) {
										dao.update(QuarterData.class,
												Chain.make("data", strdata).add("update_time", new Date()),
												Cnd.where("cn_name", "=", sub).and("yq", "=", Integer.parseInt(ndate)));
									} else {
										logger.info("数据相同不更新,:" + sub + "," + strdata);
									}
								} else {
									dao.update(QuarterData.class,
											Chain.make("data", strdata).add("update_time", new Date()),
											Cnd.where("cn_name", "=", sub).and("yq", "=", Integer.parseInt(ndate)));
									System.out.println("更新:" + sub + "," + strdata);
								}

							}

						} catch (Exception e) {
							// 别的错误
							System.out.println(e);
						}
					} else {
						PropIndex prop = dao.fetch(PropIndex.class,
								Cnd.where("cn_name", "=", sub).and("is_area", "=", "1"));

						String strdata = f1010.getJSONObject("data").getString("strdata");

						String ndate = date.replace("A", "01");
						ndate = ndate.replace("B", "04");
						ndate = ndate.replace("C", "07");
						ndate = ndate.replace("D", "10");
						QuarterAreaData tmd = new QuarterAreaData();
						tmd.setCn_name(sub);
						tmd.setCreate_time(new Date());
						tmd.setData(strdata);
						tmd.setEn_name(prop.getEn_name());
						tmd.setYq(Integer.parseInt(ndate));
						tmd.setProp(prop.getProp());
						tmd.setArea_code(area_code);
						tmd.setArea_name(area_name);
						try {
							// dao.fastInsert(gi);
							dao.fastInsert(tmd);
							logger.info(sub + "," + strdata + "," + area_name + "," + ndate);
						} catch (DaoException e) {
							// 报错就更新 (一般是重复异常)
							QuarterAreaData mm = dao.fetch(QuarterAreaData.class, Cnd.where("cn_name", "=", sub)
									.and("yq", "=", ndate).and("area_code", "=", area_code));
							if (mm == null) {
								logger.info("季度为空,:" + sub + "," + strdata);
							}
							if (StringUtils.isNotEmpty(strdata)) {
								if (mm.getData() != null) {
									if (!strdata.equals(mm.getData())) {
										dao.update(QuarterAreaData.class,
												Chain.make("data", strdata).add("update_time", new Date()),
												Cnd.where("cn_name", "=", sub).and("yq", "=", Integer.parseInt(ndate))
														.and("area_code", "=", area_code));
									} else {
										logger.info("数据相同不更新,:" + sub + "," + strdata);
									}
								} else {
									dao.update(QuarterAreaData.class,
											Chain.make("data", strdata).add("update_time", new Date()),
											Cnd.where("cn_name", "=", sub).and("yq", "=", Integer.parseInt(ndate))
													.and("area_code", "=", area_code));
									System.out.println("更新:" + sub + "," + strdata + "," + area_name);
								}

							}

						} catch (Exception e) {
							// 别的错误
							System.out.println(e);
						}

					}

				}
			}
		}
	}

}
